﻿#region Copyright Notice
/*
 
DarkLakeTools, Dark Lake Software, Inc. (Provides Performance Enhancing Tools For ASP.NET applications)
Copyright (C) 2009  Dark Lake Software, Inc. 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Dark Lake Software, Inc. 
Postal Box 916461
Longwood, FL  32791
admin@darklakesoftware.com
 
*/
#endregion

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;


namespace DarkLakeTools.PageUtility
{
    /// <summary>
    /// Improved Page Object
    /// </summary>
    public class Page : System.Web.UI.Page
    {
        private ObjectStateFormatter _formatter = new ObjectStateFormatter();

        /// <summary>
        /// Saves the page state to persistence medium.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            MemoryStream ms = new MemoryStream();
            _formatter.Serialize(ms, viewState);
            byte[] viewStateArray = ms.ToArray();
            ClientScript.RegisterHiddenField("__CVIEWSTATE",
                Convert.ToBase64String(
                CompressViewState.Compress(viewStateArray)));
        }

        /// <summary>
        /// Loads any saved view-state information to the <see cref="T:System.Web.UI.Page"/> object.
        /// </summary>
        /// <returns>The saved view state.</returns>
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string vsString = Request.Form["__CVIEWSTATE"];
            byte[] bytes = Convert.FromBase64String(vsString);
            bytes = CompressViewState.Decompress(bytes);
            return _formatter.Deserialize(
                Convert.ToBase64String(bytes));
        }

        /// <summary>
        /// Initializes the <see cref="T:System.Web.UI.HtmlTextWriter"/> object and calls on the child controls of the <see cref="T:System.Web.UI.Page"/> to render.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> that receives the page content.</param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {

            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlWriter);
            string html = stringWriter.ToString();
            int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__CVIEWSTATE\"");
            if (StartPoint >= 0)
            {
                int EndPoint = html.IndexOf("/>", StartPoint) + 2;
                string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
                html = html.Remove(StartPoint, EndPoint - StartPoint);
                int FormEndStart = html.IndexOf("</form>") - 1;
                if (FormEndStart >= 0)
                {
                    html = html.Insert(FormEndStart, viewstateInput);
                }
            }
            writer.Write(html);
        }

    }
}
